package code.rocky.chapter05;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// -Xms64m -Xmx64m
public class Suggest78 {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();
        // JVM 终止前记录内存信息
        runtime.addShutdownHook(new Thread() {
            @Override
            public void run() {
               StringBuffer sb = new StringBuffer();
                long headMaxSize = runtime.maxMemory() >> 20;
                sb.append("最大可用内存:" + headMaxSize + "M\n");
                long total = runtime.totalMemory() >> 20;
                sb.append("对内存大小: " + total + "M\n");
                long free = runtime.freeMemory() >> 20;
                sb.append("空闲内存:" + free + "M");
                System.out.println(sb);
            }
        });
        // demo01();
         demo02();
    }

    private static void demo01() {
        Map<String, String> map = new HashMap<>();
        // 放入近 40 万 键值对
        for (int i = 0; i < 393217; i++) {
            map.put("key" + i, "value" + i);
        }
    }

    private static void demo02() {
        List<String> list = new ArrayList<>();
        // 放入近 40 万 键值对
        for (int i = 0; i < 393217; i++) {
            list.add("key" + i);
        }
    }
}
